package kr.pe.johnkim.app.mobile.rest.controller;

import kr.pe.johnkim.app.domain.MyHistoryItem;
import kr.pe.johnkim.app.mobile.rest.service.MyHistoryItemService;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @author : johnkim <cale9797@gmail.com / Bettersoft Inc,.>
 * @Date : 12. 2. 6
 */
@Controller
@Path("/myHistoryItemController")
public class MyHistoryItemController {
    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(MyHistoryItemController.class);

    @Autowired
    private MyHistoryItemService myHistoryItemService;

     /**
     * 아이템 중복을 확인 한다.
     *
     * @param is
     * @return MyHistoryItem
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/checkDuplication")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryItem checkDuplication(InputStream is) throws IOException, JSONException {
        logger.info("checkDuplication");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItem = new JSONObject(line);
        JSONObject jsonItem2 = jsonItem.getJSONObject("myHistoryItem");

        MyHistoryItem parameterItem = new MyHistoryItem();

        parameterItem.setMyHistoryMembersId(jsonItem2.getString("myHistoryMembersId"));
        parameterItem.setActivateYn(jsonItem2.getString("activateYn"));
        parameterItem.setName(jsonItem2.getString("name"));

        logger.info("myHistoryMembersId : {}", parameterItem.getMyHistoryMembersId());
        logger.info("activateYn         : {}", parameterItem.getActivateYn());
        logger.info("name               : {}", parameterItem.getName());

        return myHistoryItemService.checkDuplication(parameterItem);
    }

    /**
     * 아이템을 등록 한다.
     *
     * 등록 성공 유무를 MyHistoryItem domain 에 담아서 반환 한다.
     * (SUCCESS or FAIL)
     *
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/registerItem")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryItem registerItem(InputStream is) throws IOException, JSONException {
        logger.info("registerItem");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItem = new JSONObject(line);
        JSONObject jsonItem2 = jsonItem.getJSONObject("myHistoryItem");

        MyHistoryItem parameterItem = new MyHistoryItem();

        parameterItem.setName(jsonItem2.getString("name"));
        parameterItem.setComment(jsonItem2.getString("comment"));
        parameterItem.setAmount(jsonItem2.getString("amount"));
        parameterItem.setOpenItemYn(jsonItem2.getString("openItemYn"));
        parameterItem.setFavoriteYn(jsonItem2.getString("favoriteYn"));
        parameterItem.setActivateYn(jsonItem2.getString("activateYn"));
        parameterItem.setModifyDate(jsonItem2.getString("modifyDate"));
        parameterItem.setRegisterDate(jsonItem2.getString("registerDate"));
        parameterItem.setMyHistoryMembersId(jsonItem2.getString("myHistoryMembersId"));
        parameterItem.setMyHistoryCategoriesIdMyHistoryCategories(jsonItem2.getString("myHistoryCategoriesIdMyHistoryCategories"));

        logger.info("name                                     : {}", parameterItem.getName());
        logger.info("comment                                  : {}", parameterItem.getComment());
        logger.info("amount                                   : {}", parameterItem.getAmount());
        logger.info("openItemYn                               : {}", parameterItem.getOpenItemYn());
        logger.info("favoriteYn                               : {}", parameterItem.getFavoriteYn());
        logger.info("activateYn                               : {}", parameterItem.getActivateYn());
        logger.info("modifyDate                               : {}", parameterItem.getModifyDate());
        logger.info("registerDate                             : {}", parameterItem.getRegisterDate());
        logger.info("myHistoryMembersId                       : {}", parameterItem.getMyHistoryMembersId());
        logger.info("myHistoryCategoriesIdMyHistoryCategories : {}", parameterItem.getMyHistoryCategoriesIdMyHistoryCategories());

        MyHistoryItem resultItem = new MyHistoryItem();

        if(myHistoryItemService.registerItem(parameterItem)) {
            resultItem.setResult("SUCCESS");
        } else {
            resultItem.setResult("FAIL");
        }

        return resultItem;
    }

    /**
     * Item 목록을 가져 온다.
     *
     * @param is
     * @return
     * @throws java.io.IOException
     * @throws org.codehaus.jettison.json.JSONException
     */
    @POST
    @Path("/selectItemList")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public List<MyHistoryItem> selectItemList(InputStream is) throws IOException, JSONException {
        logger.info("selectItemList");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItem = new JSONObject(line);
        JSONObject jsonItem2 = jsonItem.getJSONObject("myHistoryItem");

        MyHistoryItem parameterItem = new MyHistoryItem();

        parameterItem.setMyHistoryMembersId(jsonItem2.getString("myHistoryMembersId"));
        parameterItem.setActivateYn(jsonItem2.getString("activateYn"));
        parameterItem.setItemStart(Integer.parseInt(jsonItem2.getString("itemStart")));
        parameterItem.setItemCount(Integer.parseInt(jsonItem2.getString("itemCount")));

        logger.info("myHistoryMembersId : {}", parameterItem.getMyHistoryMembersId());
        logger.info("activateYn         : {}", parameterItem.getActivateYn());
        logger.info("itemStart          : {}", parameterItem.getItemStart());
        logger.info("itemCount          : {}", parameterItem.getItemCount());

        return myHistoryItemService.selectItemList(parameterItem);
    }

    /**
     * 아이템 내용을 가져 온다.
     *
     * @param is
     * @return MyHistoryItem
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/selectItem")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryItem selectItem(InputStream is) throws IOException, JSONException {
        logger.info("selectItem");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItem = new JSONObject(line);
        JSONObject jsonItem2 = jsonItem.getJSONObject("myHistoryItem");

        MyHistoryItem parameterItem = new MyHistoryItem();

        parameterItem.setIdMyHistoryItems(jsonItem2.getString("idMyHistoryItems"));
        parameterItem.setActivateYn(jsonItem2.getString("activateYn"));
        parameterItem.setMyHistoryMembersId(jsonItem2.getString("myHistoryMembersId"));

        parameterItem.setIdMyHistoryItems(jsonItem2.getString("idMyHistoryItems"));
        parameterItem.setActivateYn(jsonItem2.getString("activateYn"));
        parameterItem.setMyHistoryMembersId(jsonItem2.getString("myHistoryMembersId"));

        return myHistoryItemService.selectItem(parameterItem);
    }

    /**
     * 아이템 내용을 수정 한다.
     *
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/updateItem")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryItem updateItem(InputStream is) throws IOException, JSONException {
        logger.info("updateItem");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItem = new JSONObject(line);
        JSONObject jsonItem2 = jsonItem.getJSONObject("myHistoryItem");

        MyHistoryItem parameterItem = new MyHistoryItem();

        parameterItem.setIdMyHistoryItems(jsonItem2.getString("idMyHistoryItems"));
        parameterItem.setName(jsonItem2.getString("name"));
        parameterItem.setComment(jsonItem2.getString("comment"));
        parameterItem.setAmount(jsonItem2.getString("amount"));
        parameterItem.setOpenItemYn(jsonItem2.getString("openItemYn"));
        parameterItem.setFavoriteYn(jsonItem2.getString("favoriteYn"));
        parameterItem.setActivateYn(jsonItem2.getString("activateYn"));
        parameterItem.setModifyDate(jsonItem2.getString("modifyDate"));
        parameterItem.setMyHistoryMembersId(jsonItem2.getString("myHistoryMembersId"));
        parameterItem.setMyHistoryCategoriesIdMyHistoryCategories(jsonItem2.getString("myHistoryCategoriesIdMyHistoryCategories"));

        MyHistoryItem resultItem = new MyHistoryItem();

        if(myHistoryItemService.updateItem(parameterItem)) {
            resultItem.setResult("SUCCESS");
        } else {
            resultItem.setResult("FAIL");
        }

        return resultItem;
    }

    /**
     * 아이템을 비활성화 한다.
     *
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/updateDeactivateItem")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryItem updateDeactivateItem(InputStream is) throws IOException, JSONException {
        logger.info("updateDeactivateItem");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItem = new JSONObject(line);
        JSONObject jsonItem2 = jsonItem.getJSONObject("myHistoryItem");

        MyHistoryItem parameterItem = new MyHistoryItem();

        parameterItem.setIdMyHistoryItems(jsonItem2.getString("idMyHistoryItems"));
        parameterItem.setActivateYn(jsonItem2.getString("activateYn"));
        parameterItem.setModifyDate(jsonItem2.getString("modifyDate"));
        parameterItem.setMyHistoryMembersId(jsonItem2.getString("myHistoryMembersId"));

        MyHistoryItem resultItem = new MyHistoryItem();

        if(myHistoryItemService.updateDeactivateItem(parameterItem)) {
            resultItem.setResult("SUCCESS");
        } else {
            resultItem.setResult("FAIL");
        }

        return resultItem;
    }


}
